package com.example.exercise.es.doctor.service.impl;

import com.example.exercise.es.doctor.domain.Doctor;
import com.example.exercise.es.doctor.mapper.DoctorMapper;
import com.example.exercise.es.doctor.repository.DoctorRepository;
import com.example.exercise.es.doctor.service.DoctorService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Resource
    private DoctorMapper doctorMapper;
    @Resource
    private DoctorRepository doctorRepository;

    private static final Integer pageNumber = 0;
    private static final Integer pageSize = 10;
    private Pageable pageable = PageRequest.of(pageNumber,pageSize);

    @Override
    public int execSql(String path) {
        Reader fileReader = null;
        int c = 0;
        try {
            fileReader = new FileReader(path);
            String text = FileCopyUtils.copyToString(fileReader);
            for (String line : text.split("\r\n")) {
                if(line.startsWith("INSERT INTO ")) {
                    System.out.println(line);
                    if(line.endsWith(";")) {
                        line = line.substring(0, line.length()-1);
                    }
                    c += doctorMapper.execSql(line);
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return c;
    }

    @Override
    public void saveDoctor() {
        List<HashMap<String,Object>> list = doctorMapper.getDoctors();
        List<Doctor> doctors = new ArrayList<>(list.size());
        long i = 0;
        for (HashMap<String,Object> map : list) {
            Doctor doctor = new Doctor();
            doctor.setId(i);
            doctor.setDoctorId(Long.valueOf(map.get("doctorId").toString()));
            doctor.setDoctorName(map.get("doctorName").toString());
            doctor.setDoctorTitle(map.get("doctorTitle").toString());
            doctor.setDoctorDes(map.get("doctorDes").toString());
            doctor.setSpecialty(map.get("specialty").toString());
            doctor.setLabel(map.get("label").toString());
            doctor.setHospitalId(Long.valueOf(map.get("hospitalId").toString()));
            doctor.setHospitalName(map.get("hospitalName").toString());
            doctors.add(doctor);
            i++;
        }
        if(!doctors.isEmpty()){
            doctorRepository.saveAll(doctors);
        }
    }

    @Override
    public void drop() {
        doctorRepository.deleteAll();
    }

    @Override
    public Doctor findByDoctorId(Long doctorId) {
        return doctorRepository.findByDoctorId(doctorId);
    }

    @Override
    public List<Doctor> findByHospitalId(Long hospitalId) {
        return doctorRepository.findByHospitalId(hospitalId,pageable);
    }

    @Override
    public List<Doctor> findByHospitalNameOrDoctorName(String text) {
        return doctorRepository.findByHospitalNameOrDoctorName(text,text,pageable).getContent();
    }

    @Override
    public List<Doctor> searchDoctor(Integer pageNumber, Integer pageSize, String searchContent) {
        // 校验分页参数
        if (pageSize == null || pageSize <= 0) {
            pageSize = 20;
        }

        if (pageNumber == null || pageNumber < 0) {
            pageNumber = 0;
        }

        Pageable pageable = PageRequest.of(pageNumber, pageSize);
        // 构建搜索查询
        Doctor doctor = new Doctor();
        doctor.setHospitalName(searchContent);
        Page<Doctor> docPage = doctorRepository.searchSimilar(doctor, new String[]{"doctorName", "doctorDes", "hospitalName"}, pageable);
        return docPage.getContent();
    }

}
